import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import { Observable } from 'rxjs/Observable';
import { Observer } from 'rxjs/Observer';
import { map, catchError } from 'rxjs/operators';

import { Store } from '@ngrx/store';
import { State, UserState } from '../../store';
import * as fromUserState from '../../store/selectors/user.selector';

import { ResourceTreeNode } from '../components';

@Injectable()
export class ResourceTreesService {

  constructor(
    private store: Store<State>,
    private http: HttpClient
  ) { }

  getTree(type: string): Observable<ResourceTreeNode[]> {
    return this.store.select(fromUserState.getUserState)
      .pipe(
        map((state: UserState) => {
          if(state && state.info && state.info.resourceTrees) {
            return state.info.resourceTrees[ type ];
          } else {
            return [];
          }
        })
      );
  }

}
